<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item>
            <el-select 
              v-model="queryParams.statusId" 
              placeholder="状态" 
              clearable 
              style="width: 100%"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-input
              v-model="queryParams.name"
              placeholder="请输入入住人或房源地址"
              clearable
              @keyup.enter="handleQuery"
            >
              <template #append>
                <el-button :icon="Search" @click="handleQuery" />
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" style="text-align: right">
          <el-button plain type="primary" @click="handleExport">导出表格</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格区域 -->
    <el-table 
      v-loading="loading"
      :data="list"
      style="margin-top: 20px"
      border
    >
      <el-table-column label="入住人" min-width="120">
        <template #default="{ row }">
          <span>{{ row.mainRenter ? row.mainRenter.name : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" min-width="120">
        <template #default="{ row }">
          <span>{{ row.mainRenter ? row.mainRenter.contacts : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" min-width="180">
        <template #default="{ row }">
          <span>{{ row.room ? row.room.buildingName + row.room.name : '--'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="入住周期" min-width="200">
        <template #default="{ row }">
          <span>{{ row.checkInStart}} ~ {{row.checkInEnd }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150">
        <template #default="{ row }">
          <el-tag v-if="row.status === '1'" :type="row.betweenNowDays < 0 ? 'danger' : 'success'">
            入住中（{{row.betweenNowDays}}/{{row.betweenDays}}）
          </el-tag>
          <el-tag type="info" v-if="row.status === '2'">已退房</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="实名认证" prop="verifyStatus" min-width="100">
        <template #default="{ row }">
          <el-tag 
            :type="row.mainRenter && row.mainRenter.verifyStatus == '1' ? 'success' : 'info'">
            {{ row.mainRenter && row.mainRenter.verifyStatus == '1' ? '已认证' : '未认证' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分组" prop="room.groupName" min-width="120"></el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDetail(row)">详情</el-button>
          <el-button 
            link 
            type="primary" 
            @click="handleRemove(row)"
            :disabled="row.status === '1'"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination
        v-show="total > 0"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="val => { queryParams.pageSize = val; getList() }"
        @current-change="val => { queryParams.pageNum = val; getList() }"
      />
    </div>

    <!-- 详情弹窗 -->
    <LodgingDetail 
      v-if="lodgingDetailVisible" 
      v-model:visible="lodgingDetailVisible" 
      :checkInId="checkInId" 
      @cancel="() => lodgingDetailVisible = false" 
    />
  </div>
</template>

<script setup name="TenantPane">
import { Search } from '@element-plus/icons-vue'
import groupApi from '@/api/setting/group'
import checkinApi from '@/api/room/checkin'
import LodgingDetail from '@/views/room/component/lodgingDetail/index.vue'
import { getCurrentInstance } from 'vue'

const typeOptions = [{
  label: '全部',
  value: ''
}, {
  label: '整套',
  value: '1'
}, {
  label: '合租',
  value: '2'
}, {
  label: '整栋',
  value: '3'
}]

const statusOptions = [{
  label: '全部状态',
  value: ''
}, {
  label: '入住中',
  value: '3'
}, {
  label: '已退房',
  value: '4'
}]

const { proxy } = getCurrentInstance()

const groupOptions = ref([])
const list = ref([])
const loading = ref(false)
const total = ref(0)
const lodgingDetailVisible = ref(false)
const checkInId = ref('')

const data = reactive({
  queryParams: {
    typeId: null,
    statusId: null,
    name: null,
    pageNum: 1,
    pageSize: 10
  },
  staffOperration: {
    visible: false,
    type: 'add',
    form: {}
  }
})
const { queryParams, staffOperration } = toRefs(data)


/* 搜索 */
function handleQuery() {
  queryParams.value.pageNum = 1  // 搜索时重置到第一页
  getList()
}

/* 导出 */
function handleExport() {

}

/* 删除 */
function handleRemove(row) {
  if (row.status === '1') {
    proxy.$modal.alertWarning('请退房后再操作删除！')
  } else {
    checkinApi.remove(row.checkInId).then(res => {
      proxy.$modal.msgSuccess('删除成功')
    })
  }
}
/* 详情 */
function handleDetail(row) {
  checkInId.value = row.checkInId
  lodgingDetailVisible.value = true
}

/* 获取列表 */
function getList() {
  loading.value = true
  checkinApi.list(queryParams.value).then(res => {
    total.value = res.total
    list.value = res.rows
  }).finally(() => {
    loading.value = false
  })
}

// 获取分组列表
function getGroupList() {
  groupApi.getList().then(res => {
    groupOptions.value = res.rows.map(item => ({
      label: item.groupName,
      value: item.groupId
    }))
    queryParams.value.groupId = groupOptions.value[0].value
  })
}
getGroupList()
getList()
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }
}

.el-tag {
  margin-right: 5px;
}

.ml10 {
  margin-left: 10px;
}
</style>
